package com.hjt.gulimall.order.listener;

import com.hjt.gulimall.order.entity.OrderEntity;
import com.hjt.gulimall.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author:97007
 * @date:2021/10/14 16:36
 */
@RabbitListener(queues = {"order.release.queue"})
@Service
@Slf4j
public class OrderCloseListener {

    @Autowired
    private OrderService orderService;

    /**
     * 处理订单关闭的消息
     * @param order
     * @param message
     * @param channel
     * @throws IOException
     */
    @RabbitHandler
    public void testReceiveMessage(OrderEntity order, Message message, Channel channel) throws IOException {
        log.info("收到关闭订单消息:"+order.getOrderSn());
        try {
            orderService.closeOrder(order); // 关闭订单
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
}
